package com.amar.hibernate.dao;
import javax.persistence.NoResultException;
import javax.persistence.Query;

import org.springframework.util.Assert;

import com.amar.hibernate.commons.dao.GenericJpaDao;
import com.amar.hibernate.domain.StudentEntity;

public class StudentJpaDao extends GenericJpaDao<StudentEntity, Long> implements StudentDao {
	public StudentJpaDao(){
		super(StudentEntity.class);
	}

	
	public boolean checkAvailable(String userName) {
		 Assert.notNull(userName);
         
         Query query = getEntityManager()
                 .createQuery("select count(*) from " + getPersistentClass().getSimpleName() 
                                 + " u where u.userName = :userName").setParameter("userName", userName);
         
         Long count = (Long) query.getSingleResult();
         
         return count < 1;
	
		
	}


	public StudentEntity loadUserByUserName(String userName) {
		
		 Assert.notNull(userName);
         
         StudentEntity student = null;
         
         Query query = getEntityManager().createQuery("select u from " + getPersistentClass().getSimpleName()
                         + " u where u.userName = :userName").setParameter("userName", userName);
         
         try {
                 student = (StudentEntity) query.getSingleResult();
         } catch(NoResultException e) {
                 //do nothing
         }
         
         return student;
	}
}
